package com.m4f.fse.web.application;

import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Response;
import org.apache.wicket.protocol.http.HttpSessionStore;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.session.ISessionStore;
import org.apache.wicket.settings.IResourceSettings;
import org.apache.wicket.util.time.Duration;
import com.m4f.fse.adapter.gae.StaticModificationWatcher;
import com.m4f.fse.service.ObjetifyEngineService;
import com.m4f.fse.service.helper.ifc.IServiceLocator;
import com.m4f.fse.service.helper.impl.SpringServiceLocator;
import com.m4f.fse.service.helper.impl.StaticServiceLocator;

public class AdminApplication extends WebApplication {
	
	private static StaticModificationWatcher staticModificationWatcher = null;
	//private IServiceLocator serviceLocator;
	private ObjetifyEngineService engineService;
	
	@Override
	public Class<? extends Page> getHomePage() {
		return AdminHome.class;
	}
	
	public ObjetifyEngineService getEngineService() {
		return this.engineService;
	}
	
	public void setEngineService(ObjetifyEngineService eService) {
		this.engineService = eService;
	}
	
	/*public IServiceLocator getServiceLocator() {
		if(this.serviceLocator == null) {
			this.serviceLocator = StaticServiceLocator.getInstance();
		}
		return this.serviceLocator;
	}
	
	public void setServiceLocator(IServiceLocator sLocator) {
		this.serviceLocator = sLocator;
	}*/
	
	@Override
	protected void init() {
		super.init();
		mountBookmarkablePage("/index.html", AdminHome.class);
		mountBookmarkablePage("/engines/index.html", EnginesHome.class);
		IResourceSettings resourceSettings = getResourceSettings();
        if(DEVELOPMENT.equalsIgnoreCase(getConfigurationType())) {
                staticModificationWatcher = new StaticModificationWatcher();
                getResourceSettings().setResourceWatcher(staticModificationWatcher);
                getResourceSettings().setResourcePollFrequency(Duration.ONE_SECOND);
                getResourceSettings().setAddLastModifiedTimeToResourceReferenceUrl(true);
        } else if (DEPLOYMENT.equalsIgnoreCase(getConfigurationType())){
                getResourceSettings().setAddLastModifiedTimeToResourceReferenceUrl(false);
                getResourceSettings().setResourcePollFrequency(null);
        }
        /*String springConfigurationParam = getInitParameter("springConfiguration");
        this.loadSpringServiceLocator(springConfigurationParam);*/
	}
	
	/*private void loadSpringServiceLocator(String initParam) {
		Set<String> configurationFiles = new HashSet<String>();
	    StringTokenizer tok = new StringTokenizer(initParam);
	    while(tok.hasMoreTokens()) {
	      String grantedAccount = tok.nextToken();
	      configurationFiles.add(grantedAccount);
	    }
	    this.serviceLocator = new SpringServiceLocator(configurationFiles);
	}*/
	
	@Override
	protected ISessionStore newSessionStore() {   
		return new HttpSessionStore(this);  
	}
	
	
	/*@Override*/
	public RequestCycle newRequestCycle(Request request, Response response) {
		// In deploymentmode we are not using the staticModificationWatcher
	    if(staticModificationWatcher != null) {
	    	staticModificationWatcher.RunCheck();
	    }
	    return super.newRequestCycle(request, response);
	} 
	
}